package com.space.school.controller;

import com.space.school.domain.User;
import com.space.school.service.UserService;
import com.space.school.service.impl.UserServiceImpl;
import com.space.school.utils.PageBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet(name = "userServlet", value = "/userServlet")
public class UserServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //       每页显示页数
        int pageSize=3;
        String username = req.getParameter("userName");
        String status = req.getParameter("status");
        String currentPage = req.getParameter("currentPage");
        if (currentPage == null || currentPage.equals("")){
            currentPage="1";
        }
        Map<String,String[]> map = req.getParameterMap();
        PageBean<User> users = userService.search(pageSize,Integer.parseInt(currentPage),map);
        req.setAttribute("pb", users);
        req.setAttribute("userName", username);
        if(status != null&&!status.equals("")){
            req.setAttribute("status",status);
        }
        req.getRequestDispatcher("userList.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
//        String username = req.getParameter("userName");
        int pageSize=3;
        String currentPage = req.getParameter("currentPage");
        if (currentPage == null || currentPage.equals("")){
            currentPage="1";
        }
        String username = req.getParameter("userName");
        String status = req.getParameter("status");
        Map<String,String[]> map = req.getParameterMap();
        PageBean<User> users = userService.search(pageSize,Integer.parseInt(currentPage),map);
        req.setAttribute("pb", users);
        req.setAttribute("userName",username);
        if(!status.equals("")&&status != null){
            req.setAttribute("status",status);
        }
        req.getRequestDispatcher("userList.jsp").forward(req, resp);
    }
}
